/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetbi_dao;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import projetbi_objets.OClient;

/**
 * Projet BI - M2 FIC 2012-2013
 *
 * @author Jef, Tuan-Long, William, Mohamed
 */
public class DAOClient extends DAO<OClient> {

    /**
     *
     * @param obj L'objet à ajouter à la base de données <br/>
     *
     * requires <br/>
     *
     * ensures <br/>
     *   // La table en question n'est pas vide
     */
    @Override
    public OClient create(OClient obj) {

        Connection connect = this.connect;
        String sql = "INSERT INTO `client` (`idClient`, `nomClient`, `adresseClient`, `secteurActiviteClient`, `tailleClient`, `paysClient`, `langueClient`) VALUES (?, ?, ?, ?, ?, ?, ?)";
        int id = 0;
        try {
            PreparedStatement ps = (PreparedStatement) connect.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, 0);
            ps.setString(2, obj.getNomClient());
            ps.setString(3, obj.getAdresseClient());
            ps.setString(4, obj.getSecteurActiviteClient());
            ps.setInt(5, obj.getTailleClient());
            ps.setString(6, obj.getPaysClient());
            ps.setString(7, obj.getLangueClient());
            ps.executeUpdate();
            ResultSet rs = ps.getGeneratedKeys();
            if (rs.next()) {
                obj.setIdClient(rs.getInt(1));
            }

        } catch (SQLException ex) {
            Logger.getLogger(DAOClient.class.getName()).log(Level.SEVERE, null, ex);
        }


        return obj;
    }

    /**
     *
     * @param id L'identifiant de l'objet que l'on cherche dans la base <br/>
     *
     * requires <br/>
     *
     * ensures <br/>
     *
     */
    @Override
    public OClient find(int id) {
        OClient lang = new OClient();
        try {
            ResultSet result = this.connect
                    .createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE).executeQuery(
                    "SELECT * FROM client WHERE idClient = " + id);
            if (result.first()) {
                lang = new OClient(
                        id,
                        result.getString("nomClient"),
                        result.getString("adresseClient"),
                        result.getString("secteurActiviteClient"),
                        result.getInt("tailleClient"),
                        result.getString("paysClient"),
                        result.getString("langueClient"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return lang;

    }

    // Update : mise à jour d'un objet en base.
    @Override
    public OClient update(OClient obj) {
        Connection connect = this.connect;
        String sql = "UPDATE client SET nomClient = ?, adresseClient = ?, secteurActiviteClient = ?, tailleClient = ?, paysClient = ?, langueClient = ? WHERE idClient = ?";
        try {
            PreparedStatement ps = connect.prepareStatement(sql);
            ps.setString(1, obj.getNomClient());
            ps.setString(2, obj.getAdresseClient());
            ps.setString(3, obj.getSecteurActiviteClient());
            ps.setInt(4, obj.getTailleClient());
            ps.setString(5, obj.getPaysClient());
            ps.setString(6, obj.getLangueClient());
            ps.executeUpdate();
            ps.close();

        } catch (SQLException ex) {
            Logger.getLogger(DAOClient.class.getName()).log(Level.SEVERE, null, ex);
        }


        return obj;
    }

    // Delete : suppression d'un objet en base.
    @Override
    public void delete(int id) {
        Connection connect = this.connect;
        String sql = "DELETE FROM client WHERE idClient = ?";
        try {
            PreparedStatement ps = connect.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAOClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public int getMax() {
        int res = 0;
        try {
            ResultSet result = this.connect
                    .createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE).executeQuery(
                    "SELECT MAX('idClient') as id");
            if (result.first()) {
                res = result.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
}
